<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\config;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class ConfigController extends CommonController
{

    //get  admin/config   全部网站配置列表
    public function index(){
        $data = Config::orderBy('conf_order','asc')->get();
//        dd($data);
        foreach ($data as $k=>$v){
            switch ($v->field_type){
                case "input":
                    $data[$k]->_html="<input style='width:400px' type='text' name='conf_content[]' value='".$v->conf_content."'>";
//                    dd($data);
                    break;
                case "textarea":
                    $data[$k]->_html="<textarea type='text' name='conf_content[]' >".$v->conf_content."</textarea>";
//                    $data[$k]->_html="<textarea type='text' name='conf_content' value='1111'>";
//                    dd($data);
                    break;
                case "radio":
                    $arr = explode(',',$v->field_value );


                    $str='';
                    foreach($arr as $m=>$n){
                        $r = explode('|',$n );
                        $c = $v->conf_content==$r[0]?"checked":'';
                        $str.='<input type="radio" name="conf_content[]"'.$c.' value="'.$r[0].'">'.$r[1]."　";
                    }
                    $data[$k]->_html = $str;
                    break;
            }
        }
//        dd($data);
        return view('admin.config.index',compact('data'));
    }



    //post  admin/config  获取要添加的数据并存储到数据库
    public function store(){
        //验证
        $input = Input::except('_token');
        $rules = [
            'conf_name'=>'required',//定义规则，规则都是框架自带的
            'conf_title'=>'required'
        ];
        //提示信息
        $message = [
            'conf_name.required'=>'导航名称不能为空',
            'conf_title.required'=>'导航url不能为空'
        ];
        //创建验证对象，哪些post值，规则是什么，提示什么

        $validator = Validator::make($input,$rules,$message);
        //如果通过验证，
        if($validator->passes()){

            $re = Config::create($input);
            if($re){
                return redirect('admin/config');
            }else{
                return back()->with('errors','数据填入失败');
            }


        }else{

            return back()->withErrors($validator);
        }

    }

    //get. admin/config/create   添加页面的显示及数据分配
    public function create(){
        
        return view('admin.config.add');
    }

    //get admin/category/{category}
    public function show(){

    }

    //delete admin/config/{conf_id} 删除配置项
    public function destroy($conf_id){
        $re = config::where('conf_id',$conf_id)->delete();
        if($re){
            $this->putFile();
           $data = [
               'status'=>1,
               'msg'=>'配置项删除成功'
           ];
        }else{
            $data = [
                'status'=>0,
                'msg'=>'配置项删除失败'
            ];
        }

        return $data;
    }

    //put admin/config/{config}   更新自定义导航
    public function update($conf_id){
//        dd($conf_id);
        $input = Input::except('_token','_method');
//        dd($input);
        $re = config::where('conf_id',$conf_id)->update($input);
//        dd($re);
        if($re){
            $this->putFile();
            return redirect('admin/config');
        }else{
            return back()->with('errors','数据更新失败');
        }
    }

    //get admin/config/{config}/edit   显示编辑模版及分配数据
    public function edit($conf_id){
        $conf_data = config::find($conf_id);

        return view('admin.config.edit',compact('conf_data'));
    }


    //写入配置文件
    public function putFile(){
        //Config::pluck()和Config::pluck()->all()得的数据的区别,后者为纯净的数组数据
        $config = Config::pluck('conf_content','conf_name')->all();
        $path = base_path().'\Config\web.php';
        $str = '<?php return '.var_export($config,true).';';
        file_put_contents($path,$str );
//        dd($config);
    }

    //异步排序
    public function change_order(){

        $input = Input::all();

        $conf_order = $input['conf_order'];
        $conf_data = Config::find($input['conf_id']);
        $conf_data->conf_order = $conf_order;
        $re = $conf_data->update();
        if($re){
            $data=[
                'status'=>'1',
                'msg'=>"排序修改成功"
            ];
        }else{
            $data=[
                'status'=>'0',
                'msg'=>"排序修改失败"
            ];
        }

        return $data;
    }

    //改变配置内容,并写入配置文件
    public function change_content(){
        $input = Input::except('_token');
        foreach($input['conf_id'] as $k=>$v){
            Config::where('conf_id',$v)->update(['conf_content'=>$input['conf_content'][$k]]);
        }
        $this->putFile();
        return back()->with('errors','配置项配置成功');
    }


}
